<template>
  <qx-table :dbRowClick="rowDblClick" :option="tableOption" :remoteData="remoteData">
    <template v-slot:top>
      <qx-form v-model="searchForm" :option="tableOption" isSearch @submit="startSearch"></qx-form>
    </template>
    <template v-slot:menuLeft>
      <q-btn color="primary" icon="add" unelevated @click="handleCreate">
        {{ $t('Add') }}
      </q-btn>
    </template>
    <template v-slot:menuLeftR>
      <q-icon color="warning" name="mdi-map-marker-question cursor-pointer cursor-hover" size="1.5rem">
        <q-tooltip class="q-pa-md" v-bind="$tooltipAttr">{{ $t('Misc.DblClickCardTips') }}</q-tooltip>
      </q-icon>
    </template>
    <template v-slot:menu="{ row }">
      <q-btn class="col" color="primary" icon="mdi-square-edit-outline" no-wrap outline unelevated
             @click="handleUpdate(row)">
        {{ $t('Edit') }}
      </q-btn>
      <q-btn class="col" color="primary" icon="copy_all" no-wrap outline unelevated @click="handleCopy(row)">
        {{ $t('Copy') }}
      </q-btn>
      <q-btn v-if="row.isEnable === '0'" class="col" color="primary" no-wrap outline unelevated
             @click="handleIsEnable(row, '1')">
        {{ $t('Enable') }}
      </q-btn>
      <q-btn v-if="row.isEnable === '1'" class="col" color="primary" no-wrap outline unelevated
             @click="handleIsEnable(row, '0')">
        {{ $t('Disable') }}
      </q-btn>
      <q-btn v-if="false" color="primary" icon="delete" outline unelevated @click="handleDelete(row)">
        {{ $t('Del') }}
      </q-btn>
    </template>
  </qx-table>
  <q-modal v-model="copyVisible" :header="header" :width="500" @beforeClose="beforeClose" @confirm="submitForm">
    <qx-form ref="FormRef" v-model="formInfo" :option="tableOption"></qx-form>
  </q-modal>
  <q-modal v-model="modalVisible" :header="formInfo.id ? $t('Edit') : $t('Add')" :menuBtn="false" fullHeight fullWidth
           @beforeClose="beforeClose">
    <TrafficControlProcessCfg v-model="modalVisible" v-model:isRefresh="isRefresh" :rowInfo="formInfo"
                              :tcrModes="tcrModes"></TrafficControlProcessCfg>
  </q-modal>
  <q-modal v-model="infoVisible" :header="`${$t('Misc.SpecifyDetails')}`" :menuBtn="false" :persistent="false"
           width="620">
    <QInfo :columns="tableOption.column || []" :rowInfo="rowInfo" cols="2" headerWidth="100"></QInfo>
  </q-modal>
</template>
<script setup>
import { i18n, toI18nName } from 'boot/i18n.js'
import { deleteAPI, postAPI } from 'src/api/index.js'
import { getDictData, setRemoteData } from 'src/enum/ajaxRemoteData.js'
import { enableOrDisable } from 'src/enum/index.js'
import TrafficControlProcessCfg from 'src/views/RCSCore/TrafficControlProcessCfg.vue'
import { computed, onMounted, ref } from 'vue'
import { createDialog, deepClone } from 'src/utils/utils.js'

const header = ref('')
const rowInfo = ref({})
const formInfo = ref({})
const tcrModes = ref([])
const FormRef = ref(null)
const searchForm = ref({})
const isRefresh = ref(false)
const copyVisible = ref(false)
const infoVisible = ref(false)
const modalVisible = ref(false)
const remoteData = ref({ dicUrl: '/yfagv/api/tcrConfig/list' })
getDictData('AGV_TRAFFIC_CTRL_MODE').then(data => (tcrModes.value = data))
const tableOption = computed(() => {
  return {
    grid: true, span: 22, export: true, labelWidth: 120, searchSpan: 4, headerWidth: 100,
    searchLabelWidth: 80,
    column: [{
      label: `${i18n.t('Misc.ScenarioName')}`, field: 'tcrName', minWidth: 100, required: true
    }, {
      label: `${i18n.t('TaskCore.RelatedTaskTemplateCode')}`, field: 'mapName', minWidth: 100, required: true,
      search: false
    }, {
      label: `${i18n.t('AGVCore.RelatedDeviceCode')}`, field: 'deviceIndex', minWidth: 100, required: true,
      display: false
    }, {
      label: `${i18n.t('Misc.ControlMode')}`, field: 'tcrModeCode', type: 'select', minWidth: 100, required: true,
      props: { label: toI18nName('labelZhCn'), value: 'value' }, options: tcrModes.value, topLeft: true,
      display: false
    }, {
      label: `${i18n.t('MapCore.StartArea')}`, field: 'beginAreaName', minWidth: 80, search: false, display: false
    }, {
      label: `${i18n.t('MapCore.TargetArea')}`, field: 'endAreaName', minWidth: 80, search: false, display: false
    }, {
      label: `${i18n.t('MapCore.ControlPoint')}`, field: 'ctrPointCodes', minWidth: 80, search: false, hide: true,
      display: false
    }, {
      label: `${i18n.t('IsEnable')}`, field: 'isEnable', type: 'radio', searchType: 'select', value: '1',
      options: enableOrDisable.value, useTag: true, topRight: true, display: false
    }, {
      label: `${i18n.t('CreationTime')}`, field: 'createdTime', display: false, minWidth: 80, search: false, hide: true
    }, {
      label: `${i18n.t('Remarks')}`, field: 'remark', type: 'textarea', minWidth: 100, search: false, hide: true,
      display: false
    }]
  }
})

const handleCreate = () => {
  formInfo.value = {}
  modalVisible.value = true
}
const rowDblClick = row => {
  rowInfo.value = row
  infoVisible.value = true
}
const handleUpdate = row => {
  formInfo.value = deepClone(row)
  modalVisible.value = true
}
const handleCopy = row => {
  const { tcrName: name, ...params } = row
  formInfo.value = deepClone(params)
  header.value = `${i18n.t('Misc.TrafficControlCopyTips', { name })}`
  copyVisible.value = true
}
const handleIsEnable = ({ id, revision }, isEnable) => {
  const msg = isEnable === '1' ? `${i18n.t('EnableTips')}` : `${i18n.t('DisableTips')}`
  createDialog({ msg }, () => {
    postAPI('/yfagv/api/tcrConfig/enable', { id, revision, isEnable }, () => startSearch('clear'))
  }, () => {})
}
const handleDelete = row => {
  createDialog({ msg: `${i18n.t('DelTips')}` }, () => {
    deleteAPI(`/yfagv/api/tcrConfig/${row.id}`, {}, () => startSearch('clear'))
  }, () => {})
}
const formatSearchForm = () => ({ ...searchForm.value })
const startSearch = (action, done) => {
  remoteData.value.params = formatSearchForm()
  remoteData.value.clearSelected = true
  if (done) done()
  action === 'clear' && setRemoteData('traffic_control')
}
const beforeClose = () => {
  if (FormRef.value) FormRef.value.onReset()
  if (isRefresh.value) startSearch('clear')
  isRefresh.value = false
  copyVisible.value = false
  modalVisible.value = false
}
const submitForm = mDone => {
  FormRef.value.validate((form, done) => {
    const { tcrCode: fromTcrCode, mapCode, tcrName } = form
    postAPI('/yfagv/api/tcrConfig/simpleCopy', { fromTcrCode, mapCode, tcrName }, () => {
      startSearch('clear')
      done()
      mDone && mDone()
    }, () => {
      done()
      mDone && mDone(false)
    })
  }, () => mDone && mDone(false))
}
onMounted(startSearch)
</script>